package cn.yunhe.services;

/*
 * 郭慧广
 * 食物分页
 *
 * 李非凡更改
 * */
import cn.yunhe.dao.FoodFenYeDao;
import cn.yunhe.entity.merchantsentity.Food;
import cn.yunhe.util.FenYe;
import cn.yunhe.util.SqlSessionFactoryUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class FoodFenYeService {

    SqlSession sqlSession = null;
    FoodFenYeDao foodFenYeDao = null;

    public Map getFoodPage(String pageIndex, int mid) {
        SqlSession sqlSession = SqlSessionFactoryUtil.getSqlSession();
        FoodFenYeDao foodFenYeDao = sqlSession.getMapper(FoodFenYeDao.class);


        //判断当前页是否为null
        if (pageIndex == null || pageIndex == "") {
            pageIndex = "1";
        }
        int pageNum = Integer.parseInt(pageIndex);

        //总条数
        int count = foodFenYeDao.getAllCount(mid);
        //总页数
        int total = FenYe.getTotalPages(count, FenYe.pageSize);
        //页数小于1 显示第一页
        if (pageNum < 1) {
            pageNum = 1;
        }
        //页数大于总页数 显示最后一页
        if (pageNum > total) {
            pageNum = total;
        }

        //计算从第几条显示 qiShi 和显示几条数据 jieShu
        Map map = new HashMap();
        int qiShi = (pageNum - 1) * FenYe.pageSize;
        int jieShu = FenYe.pageSize;
        map.put("qiShi", qiShi);
        map.put("jieShu", jieShu);

        map.put("mid", mid);
        List<Food> foodList = foodFenYeDao.getFoodPage(map);
        Map map2 = new HashMap();
        map2.put("foodList", foodList);
        map2.put("pageNum", pageNum);
        map2.put("total", total);
        return map2;
    }

}
